#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TRGroupProvider : CY.GFive.Core.Providers.DALProvider.ITRGroupProvider
	{
		#region Sql
		private static readonly string SqlSelectTRGroup = "SELECT [Id], [TrGroupCode],[TrGoupName],[AddDate],[BuildDate] FROM [TRGroup] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTRGroup = "INSERT INTO [TRGroup]([TrGroupCode],[TrGoupName],[AddDate],[BuildDate]) values(@TrGroupCode,@TrGoupName,@AddDate,@BuildDate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTRGroup = "UPDATE [TRGroup] SET [TrGroupCode] = @TrGroupCode,[TrGoupName] = @TrGoupName,[AddDate] = @AddDate,[BuildDate] = @BuildDate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTRGroup = "DELETE [TRGroup] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTRGroup = "SELECT [Id], [TrGroupCode],[TrGoupName],[AddDate],[BuildDate] FROM [TRGroup]";
		#endregion

		#region ITRGroupProvider Members
		public Core.Business.TRGroup Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTRGroup);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TRGroup tRGroup = new Core.Business.TRGroup();

				if (!reader.IsDBNull(0)) tRGroup.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) tRGroup.TrGroupCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) tRGroup.TrGoupName = reader.GetString(2);
				if (!reader.IsDBNull(3)) tRGroup.AddDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) tRGroup.BuildDate = reader.GetDateTime(4);

				return tRGroup;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TRGroup tRGroup)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TrGroupCode", SqlDbType.NVarChar, tRGroup.TrGroupCode);
			sql.AddParameter("@TrGoupName", SqlDbType.NVarChar, tRGroup.TrGoupName);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, tRGroup.AddDate);
			sql.AddParameter("@BuildDate", SqlDbType.DateTime, tRGroup.BuildDate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTRGroup);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) tRGroup.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TRGroup tRGroup)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TrGroupCode", SqlDbType.NVarChar, tRGroup.TrGroupCode);
			sql.AddParameter("@TrGoupName", SqlDbType.NVarChar, tRGroup.TrGoupName);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, tRGroup.AddDate);
			sql.AddParameter("@BuildDate", SqlDbType.DateTime, tRGroup.BuildDate);
			sql.AddParameter("@Id", SqlDbType.Int, tRGroup.Id);
			sql.ExecuteSql(SqlUpdateTRGroup);
		}

		public void Delete(Core.Business.TRGroup tRGroup)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, tRGroup.Id);
			sql.ExecuteSql(SqlDeleteTRGroup);
		}

		public IList<Core.Business.TRGroup> GetAllTRGroup()
		{
			IList<Core.Business.TRGroup> tRGrouplist = new List<Core.Business.TRGroup>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTRGroup);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TRGroup tRGroup = new Core.Business.TRGroup();

					if (!reader.IsDBNull(0)) tRGroup.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) tRGroup.TrGroupCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) tRGroup.TrGoupName = reader.GetString(2);
					if (!reader.IsDBNull(3)) tRGroup.AddDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) tRGroup.BuildDate = reader.GetDateTime(4);

					tRGroup.MarkOld();
					tRGrouplist.Add(tRGroup);
				}
				reader.Close();
			}
			return tRGrouplist;
		}
		#endregion
	}
}
